#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "Hashtable.h"
#include "timetest.h"
using namespace std;

int main (int argc, char *argv[])
{
  if(argc != 4)
	{
      cout<<"Program not run correctly \n";
      cout<<"  How to run  $ cpu_hash database.txt queries.txt outputc.txt "<<endl;
      exit(1);
	}
  
  string str="";
  int val =0;
  ifstream database(argv[1]);
  ifstream queries(argv[2]);
  ofstream out(argv[3]);

  vector<double> q_times;
  
  Hashtable<data_val> Hash;
  
  Timer timer = Timer();
  timer.start();
  
  while(database.good())
    {
      if( database >> str)
		{
          database >> val;
          Hash.insert(str,val);
		}
	}
  double hash_pop_time = timer.stop();
  
  Timer qT = Timer();
  bool eq = false;
  while(queries.good())
    {  
      qT.start();
      if( queries  >> str)
		{
          if(str[0]== '=')
            {
              eq = true;
              q_times.push_back(qT.stop());
              out<<str<<endl;
            }
          else
            {
              eq = false;
              Hash.query(str,out);     
            }
		}
      else if(!eq)
        q_times.push_back(qT.stop());
	}
  
  out<<"***********************"<<endl;
  out<<hash_pop_time<<"s ";
  //printf("%5.6fs ", hash_pop_time);
  for(vector<double>::iterator i= q_times.begin();
      i!=q_times.end();++i)
    out<<setprecision(7)<<*i<<"s ";
  //  printf("%5.6fs ", *i);
  out<<endl;
  //  out<<"Number of collusions : " <<get_collusions()<<endl;
  out<<endl;
  out<<endl;
  // print_data(out);
  return 0;
}
